package com.seelyn.rhea.mvc.internal;

import com.seelyn.rhea.mvc.context.Request;
import com.seelyn.rhea.mvc.context.Response;
import com.seelyn.rhea.mvc.internal.matcher.RheaMatcher;
import com.seelyn.rhea.mvc.internal.matcher.UriMatcher;
import com.seelyn.rhea.mvc.route.Filter;
import com.seelyn.rhea.mvc.route.FilterMatching;

import java.util.Map;

/**
 * @author slacrey
 * @since 2016/1/24
 */
public abstract class InternalFilter implements FilterMatching {

    private final UriMatcher<String> matcher;

    protected InternalFilter(String path) {
        matcher = RheaMatcher.path(path);
    }

    @Override
    public boolean matching(String path) {
        return matcher.matching(path);
    }

    @Override
    public Map<String, String> getPathParams(String path) {
        return matcher.getPathParams(path);
    }

    static InternalFilter create(final String path, final Filter filter) {
        return new RheaInternalFilter(path, filter);
    }

    private static final class RheaInternalFilter extends InternalFilter {

        private final Filter filter;

        private RheaInternalFilter(String path, Filter filter) {
            super(path);
            this.filter = filter;
        }

        @Override
        public int order() {
            return filter.order();
        }

        @Override
        public boolean handle(Request request, Response response) {

            return filter.handle(request, response);
        }
    }
}
